using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class CTPhieuXuatHangDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveCTPhieuXuatHang(CTPhieuXuatHang ctphieuxuathang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddCTPhieuXuatHang", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddCTPhieuXuatHang";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctphieuxuathang.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdPhieuXuatHang", DbType.Int32, ParameterDirection.Input, ctphieuxuathang.IdPhieuXuatHang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TenHang", DbType.String, ParameterDirection.Input, ctphieuxuathang.TenHang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@MaHang", DbType.String, ParameterDirection.Input, ctphieuxuathang.MaHang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonViTinh", DbType.String, ParameterDirection.Input, ctphieuxuathang.DonViTinh);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonViTinhQuyDoi", DbType.String, ParameterDirection.Input, ctphieuxuathang.DonViTinhQuyDoi);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoLuong", DbType.Int64, ParameterDirection.Input, ctphieuxuathang.SoLuong);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoLuongQuyDoi", DbType.Int64, ParameterDirection.Input, ctphieuxuathang.SoLuongQuyDoi);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonGia", DbType.Int64, ParameterDirection.Input, ctphieuxuathang.DonGia);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ThanhTien", DbType.Int64, ParameterDirection.Input, ctphieuxuathang.ThanhTien);
			cmd.Parameters.Add(param);
            param = CreateParameter("@SoKg", DbType.Int64, ParameterDirection.Input, ctphieuxuathang.SoKG);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, ctphieuxuathang.IsDelete);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all CTPhieuXuatHang in DB
		public ResultObj GetAllCTPhieuXuatHang( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTPhieuXuatHang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllCTPhieuXuatHang", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTPhieuXuatHang>();
			CTPhieuXuatHang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTPhieuXuatHang();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdPhieuXuatHang = Convert.ToInt32(reader["IdPhieuXuatHang"]);
			item.TenHang = Convert.ToString(reader["TenHang"]);
			item.MaHang = Convert.ToString(reader["MaHang"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.DonViTinhQuyDoi = Convert.ToString(reader["DonViTinhQuyDoi"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.SoLuongQuyDoi = Convert.ToInt64(reader["SoLuongQuyDoi"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ThanhTien = Convert.ToInt64(reader["ThanhTien"]);
            item.SoKG = Convert.ToInt64(reader["SoKg"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all CTPhieuXuatHang in DB with condition
		public ResultObj GetAllCTPhieuXuatHangByID(CTPhieuXuatHang ctphieuxuathang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTPhieuXuatHang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllCTPhieuXuatHangByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctphieuxuathang.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTPhieuXuatHang>();
			CTPhieuXuatHang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTPhieuXuatHang();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdPhieuXuatHang = Convert.ToInt32(reader["IdPhieuXuatHang"]);
			item.TenHang = Convert.ToString(reader["TenHang"]);
			item.MaHang = Convert.ToString(reader["MaHang"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.DonViTinhQuyDoi = Convert.ToString(reader["DonViTinhQuyDoi"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.SoLuongQuyDoi = Convert.ToInt64(reader["SoLuongQuyDoi"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ThanhTien = Convert.ToInt64(reader["ThanhTien"]);
            item.SoKG = Convert.ToInt64(reader["SoKg"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a CTPhieuXuatHang out of DB with condition
		public ResultObj DeleteCTPhieuXuatHang(CTPhieuXuatHang ctphieuxuathang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteCTPhieuXuatHang", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteCTPhieuXuatHang";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctphieuxuathang.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of PhieuXuatHang from DB with condition
		public ResultObj GetPhieuXuatHang(int IdPhieuXuatHang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuXuatHang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetPhieuXuatHang", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdPhieuXuatHang);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuXuatHang>();
			PhieuXuatHang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuXuatHang();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.KhachHang = Convert.ToString(reader["KhachHang"]);
			item.ChiNhanhKhachHang = Convert.ToString(reader["ChiNhanhKhachHang"]);
			item.DiaChi = Convert.ToString(reader["DiaChi"]);
			item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
			item.MaSoThue = Convert.ToString(reader["MaSoThue"]);
			item.HinhThucThanhToan = Convert.ToString(reader["HinhThucThanhToan"]);
			item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
			item.TongTienHang = Convert.ToInt64(reader["TongTienHang"]);
			item.VAT = Convert.ToInt64(reader["VAT"]);
			item.TienVAT = Convert.ToInt64(reader["TienVAT"]);
			item.ChietKhau = Convert.ToInt64(reader["ChietKhau"]);
			item.TienChietKhau = Convert.ToInt64(reader["TienChietKhau"]);
			item.TongCong = Convert.ToInt64(reader["TongCong"]);
			item.NguoiKiemTraHang = Convert.ToString(reader["NguoiKiemTraHang"]);
			item.NgayKiemTraHang = Convert.ToDateTime(reader["NgayKiemTraHang"]);
			item.XeChoHang = Convert.ToString(reader["XeChoHang"]);
			item.SoHoaDonDo = Convert.ToString(reader["SoHoaDonDo"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
					
	}
}
